package elijah.base.activity;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import elijah.base.AppBase;
import elijah.base.thread.Scheduled;
import elijah.base.utils.ClassUtils;


public abstract class BaseActivity<V extends ViewBinding> extends AppCompatActivity {
    protected V binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (hdpi()) {
            setHdpi();
        }
        fullWindow();
        binding = (V) ClassUtils.getViewBinding(getClass(), getLayoutInflater(), 0);
        setContentView(binding.getRoot());
        init();

    }


    public void setHdpi() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        Configuration config = getResources().getConfiguration();
        dm.densityDpi = DisplayMetrics.DENSITY_HIGH;
        config.densityDpi = DisplayMetrics.DENSITY_HIGH;
        getResources().updateConfiguration(config, dm);
    }


    public boolean hdpi() {
        return getResources().getDisplayMetrics().widthPixels > 1366;
    }


    public abstract void init();

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        fullWindow();
    }


    protected void fullWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
        localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

    }


    public Context getContext() {
        return this;
    }

    @Override
    public void onBackPressed() {
        finish();
    }

}
